package com.yjhh.chdataservice.controller;

import com.yjhh.chdataservice.except.YJHHException;
import com.yjhh.chdataservice.service.FileManagerService;
import com.yjhh.chdataservice.util.CommonResultData;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Api(tags = "文件模块相关接口")
@RestController
@RequestMapping(value = "/service/fzjc/file")
@Log4j2
public class FileManageControler extends BaseControler{

    @Autowired
    private FileManagerService fileManagerService;

    /**
     * @param request            HttpServletRequest
     * @param files              多个MultipartFile
     * @param fileSrcParentPaths 文件路径
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public CommonResultData<String> handleFileUpload(HttpServletRequest request,
                                                     @RequestParam MultipartFile[] files,
                                                     @RequestParam String[] fileSrcParentPaths) {
        CommonResultData<String> commonResultData = null;
        try {
            commonResultData = fileManagerService.handleFileUpload(getRemortIP(request), files, fileSrcParentPaths);
        } catch (YJHHException e) {
            e.printStackTrace();
            log.error("handleFileUpload", e);
            commonResultData = new CommonResultData<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "upload fail");
        } catch (IOException e) {
            log.error("handleFileUpload", e);
            commonResultData = new CommonResultData<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "upload fail");
        }
        return commonResultData;
    }



}
